/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mobsandgeeks.adapters.tab;

import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * Scrollable line
 *
 * @since 2021-03-04
 */
public class ScrollableLine extends Component implements Component.DrawTask {
    private static final Color DEFAULT_COLOR = new Color(Color.getIntColor("#FF4081"));

    private float mIndicatorLineRadius;

    private int mIndicatorLineHeight;

    private RectFloat mRectF;

    private Paint mPaint;

    private float mIndicatorStartX;

    private float mIndicatorEndX;

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param indicatorStartX 起点
     * @param indicatorEndX 终点
     */
    public ScrollableLine(Context context, float indicatorStartX, float indicatorEndX) {
        super(context);
        this.mIndicatorStartX = indicatorStartX;
        this.mIndicatorEndX = indicatorEndX;
        initScrollableLine(context);
    }

    private void initScrollableLine(Context context) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(DEFAULT_COLOR);
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mRectF = new RectFloat();
        invalidate();
    }

    /**
     * 设置导航条的高度
     *
     * @param indicatorLineHeight 高度
     */
    public void setIndicatorLineHeight(int indicatorLineHeight) {
        mIndicatorLineHeight = indicatorLineHeight;
    }

    /**
     * 设置导航条的圆角
     *
     * @param indicatorLineRadius 角度
     * @return ScrollableLine
     */
    public ScrollableLine setIndicatorLineRadius(float indicatorLineRadius) {
        mIndicatorLineRadius = indicatorLineRadius;
        return this;
    }

    /**
     * 更新线条
     *
     * @param indicatorStartX 开始坐标
     * @param indicatorEndX 结束坐标
     * @param indicatorStartColor 开始颜色
     * @param indicatorEndColor 结束颜色
     * @param fraction 位移
     */
    public void updateScrollLineWidth(float indicatorStartX, float indicatorEndX, int indicatorStartColor,
            int indicatorEndColor, float fraction) {
        this.mIndicatorStartX = indicatorStartX;
        this.mIndicatorEndX = indicatorEndX;
        mPaint.setColor(new Color(Utils.evaluateColor(indicatorStartColor, indicatorEndColor, fraction)));
        invalidate();
    }

    @Override
    public void invalidate() {
        super.invalidate();
        addDrawTask(this::onDraw);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        mRectF.modify(mIndicatorStartX, 0, mIndicatorEndX, mIndicatorLineHeight);
        canvas.drawRoundRect(mRectF, mIndicatorLineRadius, mIndicatorLineRadius, mPaint);
    }
}